package controlador;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;









import modelo.Contact;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;

public class Productos implements Initializable {

	@FXML
	TextField txtNumParte;
	@FXML
	TextField txtPrecioCompra;
	@FXML
	TextArea txtDescripcion;
	@FXML
	TextField txtPrecioVenta;
	@FXML
	Label lblNumParte;
	@FXML
	Label lblDescripcion;
	@FXML
	Label lblPrecioVenta;
	@FXML
	Label lblPrecioCompra;
	@FXML
	Button btnGuardar;
	@FXML
	Button btnLimpiar;
	@FXML
	Button btnGuardarImagen;
	@FXML
	Button btnEliminar;
	@FXML
	ImageView imgProducto;
	
	@FXML private TableView<Contact> tableProductos;
	@FXML private TableColumn numParteColumn;
	@FXML private TableColumn precioCompraColumn;
	@FXML private TableColumn precioVentaColumn;
	@FXML private TableColumn descripcionColumn;
	
	ObservableList<Contact> contacts;
	
	private int posicion;
	
	
	@SuppressWarnings("unused")
	private application.Main mainApp;
	
	public void initialize1(URL arg0, ResourceBundle arg1) {
			
	}
	
	public void guardar(ActionEvent event){
		Contact contact = new Contact(txtNumParte.getText(), txtPrecioCompra.getText(), txtPrecioVenta.getText(), txtDescripcion.getText());
		contacts.add(contact);
	}
	
	public void eliminar(ActionEvent event){
		contacts.remove(posicion);
	}
	
	public void limpiar(ActionEvent event){
		txtNumParte.setText("");
		txtPrecioCompra.setText("");
		txtPrecioVenta.setText("");
		txtDescripcion.setText("");
		
	}
	
	private final ListChangeListener<Contact> indiceTabla =
            new ListChangeListener<Contact>() {
                @Override
                public void onChanged(ListChangeListener.Change<? extends Contact> c) {
                	getContacto();
                }
	};
	
	public Contact getTabla() {
        if (tableProductos != null) {
            List<Contact> tabla = tableProductos.getSelectionModel().getSelectedItems();
            if (tabla.size() == 1) {
                final Contact seleccionada = tabla.get(0);
                return seleccionada;
            }
        }
        return null;
   }	
	
	 private void getContacto() {
	        final Contact con = getTabla();
	        posicion = contacts.indexOf(con);

	        if (con != null) {
	        	
	        	txtNumParte.setText(con.getNumParte());
	        	txtPrecioCompra.setText(con.getPrecioCompra());
	        	txtPrecioVenta.setText(con.getPrecioVenta());
	        	txtDescripcion.setText(con.getDescripcion());
	        }
	    }
	
	 private void inicializarTabla(){
		 numParteColumn.setCellValueFactory(new PropertyValueFactory<Contact, String>("numParte"));
		 precioCompraColumn.setCellValueFactory(new PropertyValueFactory<Contact, String>("precioCompra"));
		 precioVentaColumn.setCellValueFactory(new PropertyValueFactory<Contact, String>("precioVenta"));
		 descripcionColumn.setCellValueFactory(new PropertyValueFactory<Contact, String>("descripcion"));
			
			contacts = FXCollections.observableArrayList();
			tableProductos.setItems(contacts);	    	
	    }
		
	public void setMain(application.Main mainApp){
		this.mainApp=mainApp;
	}
	
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		this.inicializarTabla();
		
		final ObservableList<Contact> tablaPersonaSeleccionada = tableProductos.getSelectionModel().getSelectedItems();
		tablaPersonaSeleccionada.addListener(indiceTabla);		
	}

}
